package c03.locksupport;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static util.Sleeper.sleep;

@Slf4j(topic = "c.LockSupportParkTest2")
public class LockSupportParkTest2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("t1 park ...");
            LockSupport.park();
            log.debug("after t1.interrupt(), t1.isInterrupted() = {}", Thread.currentThread().isInterrupted()); // true
            // park线程被打断也会设置打断标记
            boolean interrupted = Thread.interrupted(); // 判断当前线程是否被打断，会清除打断标记
            log.debug("判断当前线程是否被打断：{}", interrupted); // true
            log.debug("after Thread.interrupted(), t1.isInterrupted() = {}", Thread.currentThread().isInterrupted());
            // false， Thread.interrupted()会清除打断标记
            LockSupport.park(); // 由于打断标记被清除了，线程会在此等待
            log.debug("t1 unpack...");
        }, "t1");
        t1.start();

        sleep(1);
        log.debug("before t1.interrupt(), t1.isInterrupted() = {}", t1.isInterrupted());
        t1.interrupt();
    }
}
